﻿using reactiveBT.Coroutines;
using reactiveBT.Utilities;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace reactiveBT.BT.Node
{
    public class CoAction : BTNode
    {
        IEnumerator coroutine;
        System.Action fn;
        CoroutineHandle handle;
        bool enableContinue;
        public CoAction(IEnumerator co,float delay = 0,bool enableContinue = true)
        {
            coroutine = co;
            fn = null;
            handle = CoroutineManager.Instance.Fork(coroutine,delay);
            handle.Yield();
            this.enableContinue = enableContinue;
        }
        public CoAction(System.Action fn,float delay = 0, bool enableContinue = true)
        {
            coroutine = null;
            this.fn = fn;
            handle = CoroutineManager.Instance.Fork(coroutine, delay);
            handle.Yield();
            this.enableContinue = enableContinue;
        }
        public override BTState Tick(int BTid)
        {
            switch (handle.State)
            {
                case CoroutineState.Dead:
                    return BTState.Success;
                case CoroutineState.Suspended:
                    handle.Resume();
                    return enableContinue ? BTState.Continue : BTState.Success;
                default:
                    return enableContinue ? BTState.Continue : BTState.Success;
            }
        }
    }
}
